package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.ISystemNoticeService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.notice.SystemNoticeCreateValidate;
import com.mdd.admin.validate.notice.SystemNoticeValidate;
import com.mdd.admin.vo.notice.SystemNoticeDetailVo;
import com.mdd.admin.vo.notice.SystemNoticeListVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.notice.SystemNotice;
import com.mdd.common.mapper.notice.SystemNoticeMapper;
import com.mdd.common.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class SystemNoticeServiceImpl implements ISystemNoticeService {

    @Resource
    SystemNoticeMapper systemNoticeMapper;

    @Override
    public PageResult<SystemNoticeListVo> pageList(PageValidate pageValidate, SystemNoticeValidate searchValidate) {
        Integer pageNo   = pageValidate.getPage_no();
        Integer pageSize = pageValidate.getPage_size();

        MPJQueryWrapper<SystemNotice> mpjQueryWrapper = new MPJQueryWrapper<SystemNotice>()
                .selectAll(SystemNotice.class)
                .select("sa.account as username,sa.name nickname")
                .leftJoin("?_admin sa ON sa.id=t.publisher_id".replace("?_", GlobalConfig.tablePrefix))
                .eq("status",0)
                .orderByDesc("id");

        systemNoticeMapper.setSearch(mpjQueryWrapper, searchValidate, new String[]{
                "like:action:str",
                "like:username@sa.name:str",
                "like:ip:str",
                "=:type:str",
                "=:url:str",
                "datetime:start_time-end_time@t.create_time:str"
        });

        IPage<SystemNoticeListVo> iPage = systemNoticeMapper.selectJoinPage(
                new Page<>(pageNo, pageSize),
                SystemNoticeListVo.class,
                mpjQueryWrapper);

//        for (SystemNoticeListVo vo : iPage.getRecords()) {
//            vo.setCreateTime(TimeUtils.timestampToDate(vo.getCreateTime()));
//        }

        return PageResult.iPageHandle(iPage);
    }

    /**
     * 文章详情
     *
     * @author fzr
     * @param id 主键ID
     */
    @Override
    public SystemNoticeDetailVo detail(Integer id) {
        SystemNotice model = systemNoticeMapper.selectOne(
                new QueryWrapper<SystemNotice>()
                        .select(SystemNotice.class, info->
                                !info.getColumn().equals("delete_time"))
                        .eq("id", id));

        Assert.notNull(model, "文章不存在");

        SystemNoticeDetailVo vo = new SystemNoticeDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setContent(StringUtils.isNull(model.getContent()) ? "" : model.getContent());

        return vo;
    }

    /**
     * 文章新增
     *
     * @author fzr
     * @param createValidate 文章参数
     */
    @Override
    public void add(SystemNoticeCreateValidate createValidate) {
        SystemNotice model = new SystemNotice();
        BeanUtils.copyProperties(createValidate,model);
        model.setPublishTime(new Date());
        systemNoticeMapper.insert(model);
    }

    /**
     * 文章编辑
     *
     * @author fzr
     * @param updateValidate 文章参数
     */
    @Override
    public void edit(SystemNoticeCreateValidate updateValidate) {
        SystemNotice model = systemNoticeMapper.selectOne(
                new QueryWrapper<SystemNotice>()
                        .eq("id", updateValidate.getId()));

        Assert.notNull(model, "文章不存在!");
        model.setId(updateValidate.getId());
        model.setTitle(updateValidate.getTitle());
        model.setContent(updateValidate.getContent());
        systemNoticeMapper.updateById(model);
    }

    /**
     * 文章删除
     *
     * @author fzr
     * @param id 文章ID
     */
    @Override
    public void del(Integer id) {
        SystemNotice article = systemNoticeMapper.selectOne(
                new QueryWrapper<SystemNotice>()
                        .select("id, is_show")
                        .eq("id", id));

        Assert.notNull(article, "文章不存在!");
        article.setDeleteTime(new Date());
        article.setIsShow(1);
        article.setStatus(1);
        systemNoticeMapper.updateById(article);
    }

    /**
     * 文章状态
     *
     * @author fzr
     * @param id 文章主键
     */
    @Override
    public void change(Integer id) {
        SystemNotice article = systemNoticeMapper.selectOne(
                new QueryWrapper<SystemNotice>()
                        .select("id, is_show")
                        .eq("id", id)
                        .isNull("delete_time"));
        if(article.getIsShow()==0){
            article.setIsShow(1);
        }else{
            article.setIsShow(0);
        }
        Assert.notNull(article, "文章不存在!");
        systemNoticeMapper.updateById(article);
    }
}
